﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary.DataModel.WorkFlow;
using ServiceApplication.Dao.WorkFlow;
using System.Workflow.Runtime;
using System.Transactions;
using Bukovics.Workflow.Hosting;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.Activities.Rules;
using CommonLibrary.Wf.Common;
using CommonLibrary.Wf.InterFace;
using System.Xml;
using System.IO;
using CommonLibrary.Common;
using ServiceApplication.Service.WorkFlow.WorkflowDesignerApp;
using System.Workflow.Runtime.Hosting;

namespace ServiceApplication.Service.WorkFlow
{
    public class WfUtil
    {

        #region 发起工作流判断处理人

        /// <summary>
        /// 获得第一个状态的下一步状态的处理信息（第一步的发起者提交和其他提交有所不同）
        /// </summary>
        /// <param name="wfTempDic"></param>
        /// <param name="wfTemplateCode"></param>
        /// <returns></returns>
        public static ChoiceClrxx getFirstToNextClxx(Dictionary<int, StateMachineWorkflowInstance> wfTempDic, int wfTemplateCode)
        {
            等待审批提交 nextNotify = null;//返回结果变量
            StateMachineWorkflowInstance workflowInstance = wfTempDic[wfTemplateCode];
            StateActivity firstState = getFirstState(workflowInstance);
            if (firstState == null) return null;
            //= getNextStateByActivity(curretState);
            //var eveActs = firstState.Activities.Select(d => d is EventDrivenActivity).ToArray();
            //nextStateName = firstState.Name;

            string nextStateName = string.Empty;
            //2012-08-31 mlh 注释
            //尝试获取第一个state中的处理人
            //string nextStateName = firstState.Name;
            //nextNotify = getNotifyActivityByName(workflowInstance, nextStateName);
            //if (nextNotify != null) return nextNotify.处理人;

            //获取下一个state中的处理人
            foreach (Activity act3 in firstState.Activities)
            {
                //修改: 原来是EventDrivenActivity即等待发起人提交, 后因为触发事件问题
                //改为StateInitializationActivity即直接setState或者通过ifelse再setState
                //++现在更改为第一个状态也可以增加EventDriveActivity
                if (act3 is StateInitializationActivity)
                {
                    foreach (Activity act in ((StateInitializationActivity)act3).Activities)
                    {
                        if (act is IfElseActivity)
                        {
                            return new ChoiceClrxx()
                            {
                                处理人编码 = null,
                                处理者类型 = TaskClrType.按人员.ToString(),
                                处理者名称 = null,
                                按权限 = null
                            };
                        }
                        else if (act is SetStateActivity)
                        {
                            nextStateName = ((SetStateActivity)act).TargetStateName;
                        }
                    }
                }
                else if (act3 is EventDrivenActivity)
                {
                    foreach (Activity act in ((EventDrivenActivity)act3).Activities)
                    {
                        if (act is IfElseActivity)
                        {
                            return new ChoiceClrxx()
                            {
                                处理人编码 = null,
                                处理者类型 = TaskClrType.按人员.ToString(),
                                处理者名称 = null,
                                按权限 = null
                            };
                        }
                        else if (act is SetStateActivity)
                        {
                            if (((SetStateActivity)act).TargetStateName == "结束") continue;
                            nextStateName = ((SetStateActivity)act).TargetStateName;
                        }
                    }
                }
            }


            if (nextStateName == "") return null;
            nextNotify = getCurStateNotifyActivity(workflowInstance, nextStateName);
            if (nextNotify == null) return null;
            else
            {
                //nextNotify.处理人.处理人编码 = nextNotify.处理人.处理人编码;
                return nextNotify.处理人;
            }
        }

        public static StateActivity getFirstState(StateMachineWorkflowInstance ins)
        {
            String firstStateName = ins.StateMachineWorkflow.InitialStateName;
            foreach (StateActivity allState in ins.States)
            {
                if (allState.Name == firstStateName)
                {
                    return allState;
                }
            }
            return null;
        }

        #endregion
        public static EventDrivenActivity getEda4Agree(StateActivity sa)
        {
            foreach (CompositeActivity comAct in sa.Activities)
            {
                if (comAct is EventDrivenActivity)
                {
                    //通知/等待/IfElse等
                    foreach (Activity act in comAct.Activities)
                    {
                        if (act is 等待审批提交)
                        {
                            return comAct as EventDrivenActivity;
                        }
                    }
                }
            }
            return null;
        }

        //查询当前状态的同意事件下的处理人信息
        public static ChoiceClrxx getClrxx4Agree(StateActivity sa)
        {
            foreach (CompositeActivity comAct in sa.Activities)
            {
                if (comAct is EventDrivenActivity)
                {
                    //通知/等待/IfElse等
                    foreach (Activity act in comAct.Activities)
                    {
                        if (act is 等待审批提交)
                        {
                            return (act as 等待审批提交).处理人;
                        }
                    }
                }
            }
            return null;
        }

        //根据当前状态名获取下一步(同意之后的)状态的信息
        //ins 模板中对应plate的ins实例
        public static ChoiceClrxx getNextClrInfo4agree(StateMachineWorkflowInstance ins, string curStateName)
        {
            var nextNotify = getCurStateNotifyActivity(ins, curStateName);

            if (nextNotify == null) return null;
            else
            {
                nextNotify.处理人.处理人编码 = nextNotify.处理人.处理人编码;
                return nextNotify.处理人;
            }
        }

        private static string getNextStateName8Description(StateActivity sa, string description)
        {
            string result = "";
            //StateInitializationActivity||StateFinalizationActivity||EventDrivenActivity
            foreach (Activity act3 in sa.Activities)
            {
                if (act3 is EventDrivenActivity && (act3 as EventDrivenActivity).Description == description)
                {

                    foreach (Activity act in ((EventDrivenActivity)act3).Activities)
                    {
                        if (act is IfElseActivity)
                        {
                            ////IfElseBranchActivity
                            //foreach (Activity act2 in ((IfElseActivity)act).Activities)
                            //{
                            //    ActivityCondition acditon = ((IfElseBranchActivity)act2).Condition;
                            //    if (acditon != null &&
                            //        ((RuleConditionReference)acditon).ConditionName != "")
                            //    {
                            //        //SetState活动
                            //        foreach (Activity act1 in ((IfElseBranchActivity)act2).Activities)
                            //        {
                            //            if (act1 is SetStateActivity)
                            //            {
                            //                return ((SetStateActivity)act1).TargetStateName;
                            //            }
                            //        }
                            //    }
                            //}
                        }
                        else if (act is SetStateActivity)
                        {
                            return ((SetStateActivity)act).TargetStateName;
                        }
                    }
                }

            }
            return result;
        }

        public static 等待审批提交 getCurStateNotifyActivity(StateMachineWorkflowInstance ins, string currentStateName)
        {
            //第一层状态活动
            foreach (StateActivity allState in ins.States)
            {
                if (allState.Name == currentStateName)
                {
                    //StateInitializationActivity||StateFinalizationActivity||EventDrivenActivity
                    foreach (CompositeActivity comAct in allState.Activities)
                    {
                        //通知/等待/IfElse等
                        foreach (Activity act in comAct.Activities)
                        {
                            if (act is 等待审批提交)
                            {
                                return (等待审批提交)act;
                            }
                        }
                    }
                }
            }
            return null;
        }

        private static 加签 getRefuseActivityByName(StateMachineWorkflowInstance ins, string currentStateName, String descript)
        {
            //第一层状态活动
            foreach (StateActivity allState in ins.States)
            {
                if (allState.Name == currentStateName)
                {
                    //StateInitializationActivity||StateFinalizationActivity||EventDrivenActivity
                    foreach (CompositeActivity comAct in allState.Activities)
                    {
                        if (((EventDrivenActivity)comAct).Description == descript)
                        {
                            //通知/等待/IfElse等
                            foreach (Activity act in comAct.Activities)
                            {
                                if (act is 加签)
                                {
                                    return (加签)act;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }

        #region 处理过程判断处理人
        /// <summary>
        /// 获得下一步的处理人信息
        /// </summary>
        /// <param name="wfTempleteCode"></param>
        /// <param name="currentTaskName"></param>
        /// <returns></returns>
        public static ChoiceClrxx getNextClxx(Dictionary<int, StateMachineWorkflowInstance> wfTempDic, int wfTempleteCode, string currentStateName, string description)
        {
            StateMachineWorkflowInstance workflowInstance = wfTempDic[wfTempleteCode];
            StateActivity curretState = getStateByStateName(workflowInstance, currentStateName);
            if (curretState == null) return null;
            string nextStateName = getNextStateName8Description(curretState, description);
            if (nextStateName == "" || nextStateName == "结束") return null;
            等待审批提交 nextNotify = getCurStateNotifyActivity(workflowInstance, nextStateName);
            if (nextNotify == null) return null;
            else
            {
                nextNotify.处理人.处理人编码 = nextNotify.处理人.处理人编码;
                return nextNotify.处理人;
            }
        }

        public static 等待审批提交 getNextStateEventriven4agree(StateMachineWorkflowInstance ins, string currentStateName, string description)
        {
            StateActivity curretState = getStateByStateName(ins, currentStateName);
            if (curretState == null) return null;
            string nextStateName = getNextStateName8Description(curretState, description);
            if (nextStateName == "" || nextStateName == "结束") return null;
            等待审批提交 nextNotify = getCurStateNotifyActivity(ins, nextStateName);
            return nextNotify;
        }

        public static Boolean taskRefuse(Dictionary<int, StateMachineWorkflowInstance> wfTempDic, int wfTempleteCode, string currentStateName, string description)
        {
            StateMachineWorkflowInstance workflowInstance = wfTempDic[wfTempleteCode];
            加签 nextNotify = getRefuseActivityByName(workflowInstance, currentStateName, description);
            if (nextNotify == null) return false;
            else
                return true;
        }

        public static StateActivity getStateByStateName(StateMachineWorkflowInstance ins, string currentStateName)
        {
            //第一层状态活动
            foreach (StateActivity allState in ins.States)
            {
                if (allState.Name == currentStateName)
                    return allState;

            }
            return null;
        }
        #endregion

        public StateActivity getState8StateName(StateMachineWorkflowInstance ins, string stateName)
        {
            foreach (StateActivity allState in ins.States)
            {
                if (allState.Name == stateName)
                    return allState;
            }
            return null;
        }

    }

}
